package thread;

public class ThreadDemo16 {
    public static void main(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();

        //给两个锁编号：1 、2，规定locker1是1号，locker2是2号，按照从小到大的顺序拿
        Thread t1 = new Thread(() -> {
            //先拿序号小的
            synchronized (locker1) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //后拿序号大的
                synchronized (locker2) {
                    System.out.println("线程t1拿到两个锁");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (locker1) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2) {
                    System.out.println("线程t2拿到两个锁");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
